package com.hqq.exercise.math;


/**
 * NumberPower 数值的整数次方
 * 题目描述:
 * 实现函数double Power(double case,int exponent)
 * 求base的exponent次方
 * Created by heqianqian on 2017/8/10.
 */
public class NumberPower {

    public static double power(double number, int exponent) {
        //if (!checkNumberTooSmall(number)) {
        //    throw new RuntimeException("参数不合法");
        //}
        if (exponent >= 0) {
            return powerWithExponent(number, exponent);
        } else {
            return 1 / powerWithExponent(number, -exponent);
        }
    }

    private static double powerWithExponent(double number, int exponent) {
        double result = 1;
        if (exponent == 0) {
            return 1;
        }
        if (exponent == 1) {
            return number;
        }
        for (int i = 0; i < exponent; i++) {
            result *= number;
        }
        return result;
    }

    //private static boolean checkNumberTooSmall(double number) {
    //    return Math.abs(number) <= 0.0000001;
    //}
}
